// packageC/pages/coups/signUp.js
var common = require("../../../public.js");
Page({

    /**
     * 页面的初始数据
     */
    data: {
        details: '',
        sign: [{
            name: '开始报名',
            signTime: '5月15日',
            select: true
        },
        {
            name: '开始报名',
            signTime: '5月15日',
            select: false
        },
        {
            name: '开始报名',
            signTime: '5月15日',
            select: false
        },
        ],
        objss: '', //状态对象
        activId: '',
        type: 0,
        bookCoupList: [], //券列表
        statusFn: '',
        enrollBeginTime: '', //开始报名时间
        enrollEndTime: '', //截止报名时间
        lotteryTime: '', //开奖日期
        time: 30 * 60 * 60 * 1000,
        BaoBegin: '',
        DrawAprizeTime: '',
        baoOne: false, //开始报名
        baoTwo: false, //截止报名
        baoTwoTouMing: false,
        baoThree: false, //开奖日期
        bottomTouMing: true, //true可报名
        oneTime: '', //报名倒计时
        twoTime: '',
        timeData: {},
        threeTime: '',
        activeFlagone: true,
        activeFlagtwo: true,
        activeFlagthree: true,
        imgUrl: "",
        isOnline: '',
        timer: '',
        isYong:true
    },
    onChange(e) {
        this.setData({
            timeData: e.detail,
        });
    },
    goWinPrize() {
        wx.navigateTo({
            url: `/packageC/pages/List/winPriceList?activId=${this.data.activId}`,
        })
    },

    goSign() {
        const that = this
        if (!that.data.bottomTouMing) {
            wx.showToast({
                title: '不可报名',
                icon: 'none'
            })
            return
        }
        wx.removeStorageSync("djsendtime");
        console.log(this.data.timer)
        clearInterval(this.data.timer);
        wx.navigateTo({
            url: `/packageC/pages/coups/toSignUp?activId=${this.data.activId}&twoTime=${this.data.twoTime}&imgUrl=${this.data.imgUrl}&isOnline=${this.data.isOnline}`,
        })
    },

    // 活动介绍
    activeFn() {
        const that = this
        let temp = {
            activId: this.data.activId,
            isOnline: this.data.isOnline
        }
        common.post('/activExplain/content', temp, function (res) {
            if (res.status === 200) {
                console.log(res.data, "54554774414144")
                that.setData({
                    details: res.data
                })
            } else {
                common.showToast(res.msg)
            }
        })
    },

    totoalFn(obj) {
        const that = this
        let {
            enrollBeginTime,
            enrollEndTime,
            lotteryTime,
            status
        } = obj
        const nowDate = new Date().getTime(); //当前时间
        if (((nowDate - enrollBeginTime) / 1000) >= 0) { //现在时间超过开始报名的时间段 ，并且当前时间不能超过截止报名时间点
            console.log("开始报名")
            // 开始报名的标签
            that.setData({
                baoOne: true,
                bottomTouMing: true,
            })
            if (((nowDate - enrollEndTime) / 1000) >= 0) {
                console.log("活动截止")
                that.setData({
                    activeFlagone: false,
                    activeFlagtwo: false,
                    baoOne: false,
                    baoTwo: true,
                    bottomTouMing: false
                }, () => {
                    if (status == 0) {
                        that.kaijiang(nowDate, lotteryTime, 0)
                    } else if (status == 1) { //开奖
                        that.kaijiang(nowDate, lotteryTime)
                    } else if (status == 2) {
                        that.kaijiang(nowDate, lotteryTime)
                    }
                })
            } else {
                console.log("活动报名开始")
                let twoTime = enrollEndTime - nowDate //活动未开始的时间倒计时
                if (status == 0) {
                    let c = twoTime / 1000 //结束
                    var djsendtime = wx.getStorageSync('djsendtime');
                    if (djsendtime != undefined && !isNaN(djsendtime) && djsendtime != '') {
                        var locksecends = parseInt((djsendtime - nowDate) / 1000)
                        //   console.log(djsendtime ,"1111111")
                        if (locksecends <= 0) {
                            wx.removeStorageSync("djsendtime");
                        } else {
                            this.LockButton(locksecends);
                        }
                    } else {
                        this.LockButton(c);
                    }
                }
                that.setData({
                    activeFlagone: false,
                    activeFlagtwo: true,
                    bottomTouMing: true,
                    twoTime
                    // activeFlagthree:false
                }, () => { //开奖
                    if (status == 1) {
                        that.kaijiang(nowDate, lotteryTime)
                    } else if (status == 2) {
                        that.kaijiang(nowDate, lotteryTime)
                    }
                })
            }
        } else {
            console.log("未开始报名")
            let oneTime = enrollBeginTime - nowDate //活动未开始的时间倒计时
            console.log(oneTime, "oneTimeoneTimeoneTimeoneTimeoneTime")
            let cc = oneTime / 1000 //结束
            var djsendtime = wx.getStorageSync('djsendtime');
            if (djsendtime != undefined && !isNaN(djsendtime) && djsendtime != '') {
                var locksecends = parseInt((djsendtime - nowDate) / 1000)
                //   console.log(djsendtime ,"1111111")
                if (locksecends <= 0) {
                    wx.removeStorageSync("djsendtime");
                } else {
                    this.LockButton(locksecends);
                }
            } else {
                this.LockButton(cc);
            }
            that.setData({
                activeFlagone: true,
                activeFlagtwo: false,
                bottomTouMing: false,
                oneTime
            })
        }
    },
    // 开奖
    kaijiang(nowDate, lotteryTime, status) {
        const that = this
        if (status == 0) {
            if (((nowDate - lotteryTime) / 1000) >= 0) {
                console.log("开奖日期222")  
                that.setData({
                    baoThree: true,
                    baoTwoTouMing: true,
                    bottomTouMing: false,
                    activeFlagone: false,
                    activeFlagtwo: false,
                    activeFlagthree: true,
                    isYong:false
                },()=>{
                   
                        that.StatusFn()
                  
                })
            }
            return
        }
        if (((nowDate - lotteryTime) / 1000) >= 0) {
            console.log("开奖日期6666")
            // that.StatusFn()
            that.setData({
                baoThree: true,
                baoTwoTouMing: true,
                bottomTouMing: true,
                // activeFlagone: false,
                activeFlagtwo: true,
                activeFlagthree: true,
                isYong:false
            },()=>{
              
                    that.StatusFn()
               
            })
        } else {
            console.log("未到开奖日期")
            let threeTime = lotteryTime - nowDate //活动未开始的时间倒计时
            let ccc = parseInt((lotteryTime - nowDate) / 1000) //结束
            var djsendtime = wx.getStorageSync('djsendtime');
            if (djsendtime != undefined && !isNaN(djsendtime) && djsendtime != '') {
                var locksecends = parseInt((djsendtime - nowDate) / 1000)
                //   console.log(djsendtime ,"1111111")
                if (locksecends <= 0) {
                    wx.removeStorageSync("djsendtime");
                } else {
                    this.LockButton(locksecends);
                }
            } else {
                this.LockButton(ccc);
            }
            that.setData({
                baoThree: false,
                bottomTouMing: false,
                // activeFlagone: false,
                activeFlagtwo: true,
                threeTime,
                activeFlagthree: false
            })
        }
    },

    // 检查状态
    StatusFn() {
        let that = this
        let temp = {
            activId: that.data.activId,
            userId: that.data.userInfo.id,
        }
        common.post('/adminCouponCon/apps/getMyBookCouponStatus', temp, function (res) {
            if (res.status === 200) {
                console.log(common.dateFormat('yyyy-MM-dd HH:mm:ss', res.data.lotteryTime), "小时候小时候")
                // enrollBeginTime开始报名时间，截止倒计时
                if(that.data.isYong){
                    that.totoalFn(res.data)
                }
              
                that.setData({
                    objss: res.data,
                    statusFn: res.data.status,
                    enrollBeginTime: common.dateFormat('yyyy-MM-dd HH:mm:ss', res.data.enrollBeginTime),
                    enrollPromise: res.data.enrollPromise,
                    enrollEndTime: common.dateFormat('yyyy-MM-dd HH:mm:ss', res.data.enrollEndTime),
                    lotteryTime: common.dateFormat('yyyy-MM-dd HH:mm:ss', res.data.lotteryTime),
                })
            } else {
                common.showToast(res.msg)
            }
        })
    },
    //时间倒计时
    LockButton: function (locksecends) {
        console.log(locksecends)
        let that = this
        var djsendtime = wx.getStorageSync('djsendtime') //结束时间
        if (djsendtime == null || djsendtime == undefined || djsendtime == 'undefined' || djsendtime == 'null' || djsendtime == '') {
            var now = new Date().getTime(); //当前时间戳
            var endtime = locksecends * 1000 + now; //结束时间戳
            wx.setStorageSync('djsendtime', endtime); //将结束时间保存到cookie
        }
        this.timer = setInterval(function () {
            locksecends--;
            console.log(locksecends, "-----------------------")
            if (locksecends <= 0) {
                console.log("刷新页面")
                //倒计时结束清除 localStorage值
                wx.removeStorageSync("djsendtime");
                clearInterval(that.timer);
                that.totoalFn(that.data.objss)
            } else {

            }
        }, 1000)


    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var that = this
        console.log("1111111")
        console.log(options)
        // let objs = wx.getStorageSync('obj')
        // if (objs == null || objs == undefined || objs == 'undefined' || objs == 'null' || objs == '') {
        var obj = {
            isOnline: options.isOnline ? options.isOnline : 0,
            activId: options.activId ? options.activId : 69
        }
        wx.setStorageSync('obj', obj); //将结束时间保存到cookie
        //    }

        common.getLocalUserInfo(function (success, data) {
            if (success) {
                that.setData({
                    userInfo: data.user,
                    token: data.token,
                    imgUrl: options.imgUrl ? options.imgUrl : '',
                    // isOnline: options.isOnline ? options.isOnline : '',
                    // activId: options.activId ? options.activId : 68
                }, function () {


                    // that.coupFn()
                })
            } else {
                that.setData({
                    userInfo: null,
                    token: '',
                })
            }
        }, true)
    },
    onShow() {
        const that = this
        let objs = wx.getStorageSync('obj')
        this.setData({
            isOnline: objs.isOnline,
            activId: objs.activId
        }, () => {
            that.coupList()
            that.StatusFn()
            that.activeFn()
        })

    },
    // 券的列表
    coupList() {
        let that = this
        let temp = {
            activId: that.data.activId,
            token: that.data.token
        }
        common.post('/adminCouponCon/apps/enrollAdminCouponsByActicv', temp, function (res) {
            if (res.status === 200) {
                console.log(res.data, "11111")
                that.setData({
                    bookCoupList: res.data
                })
            } else { }
        })
    },
    /**
 * 生命周期函数--监听页面隐藏
 */
    onHide() {
        console.log("隐藏了")
        //   清除定时器
        wx.removeStorageSync("djsendtime");
        console.log(this.data.timer)
        clearInterval(this.data.timer);

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {
        console.log("卸载了")
        // 清除定时器
        wx.removeStorageSync("djsendtime");
        console.log(this.data.timer)
        clearInterval(this.timer);
        clearInterval(this.data.timer);
    },
})